package com.ifp.weixin.biz.core.impl;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import ywpt.core.web.BaseAction;

import com.ifp.weixin.MessageMng.Service.LocationMessageService;
import com.ifp.weixin.MessageMng.Service.RespArticleInfoDefineService;
import com.ifp.weixin.MessageMng.Service.RespReqdealRelaService;
import com.ifp.weixin.biz.core.BusLineSearch;
import com.ifp.weixin.biz.core.CoreService;

import com.ifp.weixin.util.RequestParse;
import com.ifp.weixin.util.SignUtil;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class CoreServiceAction extends BaseAction implements ModelDriven,
		Preparable {

	@Resource(name="coreService")  //名称一致注入
	private CoreService coreService;

	
	public void  switchOn() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest  request =  ServletActionContext.getRequest();
		
		String methodString = request.getMethod();
		if ("GET".equals(methodString) ) {
			get(request,response);
			
		} else if ( "POST".equals(methodString) ) {
			post(request,response);
			
		}
	}
	
	public void get(HttpServletRequest request, HttpServletResponse response) {
		// 微信加密签名，signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
		System.out.println(new Date() +": recived get http msg!  " );
		coreService.haha();
		String signature = request.getParameter("signature");
		// 时间戳
		String timestamp = request.getParameter("timestamp");
		// 随机数
		String nonce = request.getParameter("nonce");
		// 随机字符串
		String echostr = request.getParameter("echostr");
		
		

		PrintWriter out = null;
		try {
			out = response.getWriter();
			// 通过检验signature对请求进行校验，若校验成功则原样返回echostr，否则接入失败
			if (SignUtil.checkSignature(signature, timestamp, nonce)) {
				out.print(echostr);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			out.close();
			out = null;
		}
	}
	
	
	
public void post(HttpServletRequest request, HttpServletResponse response) {
		
		System.out.println(new Date() +"recived post http msg!  " );
		if (null == coreService)
			System.out.println(" coreService == null");
		coreService.haha();
		try {
			System.out.println("111111111!");
			request.setCharacterEncoding("UTF-8");
			System.out.println("222222222!");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		System.out.println("333333333!");
		response.setCharacterEncoding("UTF-8");
		System.out.println("444444444!");
		// 调用核心业务类接收消息、处理消息
		String respMessage = coreService.processRequest(request);

		// 响应消息
		PrintWriter out = null;
		if ( respMessage !=null && !respMessage.equals("")) {
			try {
				out = response.getWriter();
				out.print(respMessage);
				System.out.println(new Date() +" : respMessage =  "+ respMessage );
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				out.close();
				out = null;
			}
		} else {
			try {
				StringBuffer sb= new StringBuffer();
				sb.append( "对不起系统异常！请稍后再试；" );
					
				respMessage = sb.toString();
				out = response.getWriter();
				out.print(respMessage);
				
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				out.close();
				out = null;
			}
		}
	}


	public Object getModel() {
		// TODO 自动生成方法存根
		return null;
	}

	public void prepare() throws Exception {
		// TODO 自动生成方法存根

	}

	/**
	 * @return coreService
	 */
	public CoreService getCoreService() {
		return coreService;
	}

	/**
	 * @param coreService 要设置的 coreService
	 */
	public void setCoreService(CoreService coreService) {
		this.coreService = coreService;
	}

}
